package com.situ.swing111;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class MainFrameTeatch extends JFrame {
	private TeatchTableModel teatchTableModel;
	private JTable table;
	private List<Teatch> lists;
	
	
	public static void main(String[] args) {
		MainFrameTeatch mainFrame = new MainFrameTeatch();
		mainFrame.init();
		
	}
	
	
	
	public void init() {
		this.setSize(800, 600);
		// mainFrame.setLocation(300, 50);
		SwingUtil.setLocationCenter(this);
//		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//只销毁本窗口

		JPanel mainPanel = (JPanel) this.getContentPane();
		BoxLayout boxLayout = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
		mainPanel.setLayout(boxLayout);
		// 窗口从上往下第一部分
		JPanel topPanel = new JPanel();
		// 设置布局之间的间隙
		topPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
		// 姓名
		JLabel nameLabel = new JLabel("姓名： ");
		JTextField nameText = new JTextField();
		nameText.setPreferredSize(new Dimension(130, 30));
		topPanel.add(nameLabel);
		topPanel.add(nameText);
		// 搜索按钮
		JButton searchButton = new JButton("搜索");
		searchButton.setPreferredSize(new Dimension(80, 30));
		topPanel.add(searchButton);

		// 窗口从上往下第二部分，主要是table表格
		JPanel centerPanel = new JPanel();
		centerPanel.setBackground(Color.BLACK);

		table = new JTable();
		table.setRowHeight(45);
		List<Teatch> lists = loadDatas();

		teatchTableModel = new TeatchTableModel(lists);
		table.setModel(teatchTableModel);
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setPreferredSize(new Dimension(300, 50));
		centerPanel.setLayout(new BorderLayout());
		centerPanel.add(scrollPane, BorderLayout.CENTER);

		// 窗口从上往下第三部分, 新增、修改、删除三个按钮
		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 80, 10));
		// addButton
		JButton addButton = new JButton("新增");
		addButton.setPreferredSize(new Dimension(80, 30));
		addButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				AddTeatchFrame addTeatchFrame = new AddTeatchFrame(MainFrameTeatch.this);
				addTeatchFrame.init();
			}
		});
		bottomPanel.add(addButton);
		// updateButton
		JButton updateButton = new JButton("修改");
		updateButton.setPreferredSize(new Dimension(80, 30));
		bottomPanel.add(updateButton);
		// deleteButton
		JButton deleteButton = new JButton("删除");
		deleteButton.setPreferredSize(new Dimension(80, 30));
		deleteButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// 选中的所有行的索引
				int[] indices = table.getSelectedRows();
				if (indices.length == 0) {// 未选中任何行
					JOptionPane.showMessageDialog(MainFrameTeatch.this, "请选择您要删除的行", "提示", JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				for (int i = 0; i < indices.length; i++) {
					Integer id1 = (Integer) table.getValueAt(indices[i], 0);
					Iterator<Teatch> iterator = lists.iterator();
					while (iterator.hasNext()) {
						Teatch teatch = iterator.next();
						if (teatch.getId1() == id1) {
							iterator.remove();
						}
					}
				}

				MainFrameTeatch.this.reloadTable();
			}
		});
		bottomPanel.add(deleteButton);

		mainPanel.add(topPanel);
		mainPanel.add(centerPanel);
		mainPanel.add(bottomPanel);

		this.setVisible(true);
	}

	private List<Teatch> loadDatas() {
		lists = new ArrayList<>();
		Teatch teatch1 = new Teatch(1801, "李四1", 27, "男",9800);
		Teatch teatch2 = new Teatch(1802, "李四2", 29, "女",8999);
		Teatch teatch3 = new Teatch(1803, "李四3", 27, "男",10000);
		Teatch teatch4 = new Teatch(1804, "李四4", 26, "女",9900);
		Teatch teatch5 = new Teatch(1805, "李四5", 30, "男",9800);
		Teatch teatch6 = new Teatch(1806, "李四6", 33, "女",20000);
		Teatch teatch7 = new Teatch(1807, "李四7", 35, "男",8990);
		Teatch teatch8 = new Teatch(1808, "李四8", 37, "女",10200);
		Teatch teatch9 = new Teatch(1809, "李四9", 36, "男",40000);
		lists.add(teatch1);
		lists.add(teatch2);
		lists.add(teatch3);
		lists.add(teatch4);
		lists.add(teatch5);
		lists.add(teatch6);
		lists.add(teatch7);
		lists.add(teatch8);
		lists.add(teatch9);
		return lists;
	}
	
	public List<Teatch> getData() {
		return lists;
	}
	
	/*
	 * 重新加载表格数据
	 */
	public void reloadTable() {
		teatchTableModel.setData(lists);
		teatchTableModel.fireTableDataChanged();// 触发表格数据变更事件
	}
}
